-- CrystalBar
-- Created by huanghr Apr/14/2016
-- 水晶结界

-- 初始化
function initCrystalBar(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local pos = self.gridData:getPos();
    local function doEvent()
        DungeonActionM.go("active_crystal", pos);
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local iconName = "stone_light";

            if self.gridData.actived == true then
                iconName = "stone_light";
                local iconPath = getLevelItemIconPath(iconName);
                self:openBuildingView(nil, nil, nil, getLocStr("btn_text_leave"), nil, iconPath);
            else
                iconName = "stone_light2";
                local iconPath = getLevelItemIconPath(iconName);
                -- 打开水晶结界界面
                self:openBuildingView(doEvent, nil, nil, nil, nil, iconPath);
            end

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end


    local iconPath = getLevelItemIconPath("stone_light2");
    local icon = ccui.ImageView:create(iconPath);
    icon:setName("icon");
    icon:setTouchEnabled(true);
    icon:addTouchEventListener(onClicked);
    self.item:addChild(icon);
    -- self.icon:setOpacity(0);

    -- -- 创建石柱模型，初始停在第一帧
    -- local effectId = iif(stoneType == STONE_TYPE_LIGHT, 1197, 1192);
    -- local animation = createEffectAnim(effectId);
    -- animation:gotoFrameAndPause(0);
    -- self.item.animation = animation;
    -- self.item:addChild(animation);

    self.item:setPositionY(10);
end

-- 激活光效
function onCrystalActive(self)
    -- 显示全屏星光特效
    showScreenShining();

    -- 创建石柱模型，初始停在第一帧
    local effectId = 1197;
    local animation = createEffectAnim(effectId);
    animation:gotoFrameAndPause(0);
    self.item.animation = animation;
    self.item:addChild(animation);
    local icon = findChildByName(self.item, "icon");
    icon:setOpacity(0);

    playCrystalEffect(self);
end